import json
import os
import pprint
import pytest
import requests
from common.read_yaml import ReadYamlData,ReadYamlConfigs
from common.readconfig import conf
import configparser

@pytest.mark.parametrize()
def get_cookie():

    Cookie_url = "http://test.api.7hi.tv/auth/fast-login"
    Cookie_data = {
            "appVersion": "debug3.3.3_test(build333)",
            "channel": "gw",
            "defaultReg": "Y",
            "mobile": "18183842316",
            "phoneModels": "HUAWEI TAS-AN00",
            "systemName": "android",
            "systemVersion": "29",
            "type": "SYS",
            "validateCode": "999999"
    }
    data = json.dumps(Cookie_data)
    header = {"Content-Type": "application/json"}
    r = requests.post(url=Cookie_url,data=data,headers=header)
    result_json = r.json()
    id = result_json['data']['userInfo']['id']
    cookie = r.cookies.get_dict()
    cookie = '; '.join([f"{k}={v}" for k, v in cookie.items()])
    conf_file = os.path.abspath(os.path.dirname(os.path.dirname(__file__)) + r"\config\token.ini")  # 保存文件路径
    config = configparser.ConfigParser()
    config.read(conf_file,encoding='utf-8')
    config.set("cookie",str(id),str(cookie))
    config.write(open(conf_file,'w'))
    return id

@pytest.mark.parametrize()
def send_gift():
    url = ReadYamlConfigs("data.yaml").get_yaml_data()['HOST'] + '/liveshowcenter/api/sendNewGift'
    payload = {"actorId": "30133244","batteryTypeList": [1],"giftId": "518","giftNumber": "1","giftSource": 1,"liveType": 1,"roomId": "11470","secretFlag": 0,"userId": 30130675}
    headers = {
        'Content-Type':'application/json; charset=UTF-8',
        'Cookie':conf.get('cookie',payload['batteryTypeList']['actorId'])
        # 'Cookie':get_cookie()
    }
    data = json.dumps(payload)
    r = requests.request("POST",url,headers=headers,data=data)
    print(r.text)
    pprint.pprint(r.text)


if __name__ == '__main__':
    print(get_cookie())
    # print(send_gift())

#     # send_gift()